/**
 * 链表结构 - start
 */
class Node {
  constructor(value) {
    this.val = value;
    this.next = null;
  }
}

class LinkedList {
  constructor() {
    this.head = null;
  }
  append(value) {
    let newNode = new Node(value);
    if (!this.head) {
      this.head = newNode;
    } else {
      let current = this.head;
      while (current.next) {
        current = current.next;
      }
      current.next = newNode;
    }
  }
}
/**
 * 链表结构 - end
 */
// 测试用例 - demo - [1,2,3];
let demo = new LinkedList();
demo.append(1);
demo.append(2);
demo.append(3);
demo.append(4);

// ----------------------------------------------
// ----------------------------------------------
// ----------------------------------------------
// ----------------------------------------------
//  输入一个链表，反转链表后，输出新链表的表头。
const ReverseList = (head) => {
  // 上一个节点
  let previous = null;
  while (head) {
    // 当前节点
    let current = head;
    // 让head指向下一个节点
    head = head.next;
    // 反转指向前一个
    current.next = previous;
    // 当前节点current就是下一轮的上一个节点 保存在previous
    previous = current;
  }
  return previous;
};

console.log(ReverseList(demo.head));
